import java.util.*;

public class Answer2 {
    //岛屿数量
    //https://www.nowcoder.com/practice/0c9664d1554e466aa107d899418e814e?tpId=196&tqId=37167&ru=/exam/oj

    static int[] dx = {1, -1, 0, 0};
    static int[] dy = {0, 0, 1, -1};
    static boolean[][] dp;
    static int n;
    static int m;


    public static void main(String[] args) {
        char[][] grid=new char[][]{{1,1,0,0,0}, {0,1,0,1,1},{0,0,0,1,1},{0,0,0,0,0},{0,0,1,1,1}};
        System.out.println(solve(grid));
    }
    public static int solve (char[][] grid) {
        n = grid.length;
        m = grid[0].length;
        dp = new boolean[n][m];
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1 && !dp[i][j]) {
                    check(grid, i, j);
                    count++;
                }
            }
        }

        return count;
    }

    public static void check(char[][] grid, int i, int j) {
        Stack<int[]> stack = new Stack<>();
        stack.add(new int[] {i, j});
        while (!stack.isEmpty()) {
            int[] c= stack.pop();

            for (int a = 0; a < 4; a++) {
                int x = dx[a] + c[0];
                int y = dy[a] + c[1];
                if (x < n && x >= 0 && y >= 0 && y < m && grid[x][y] == 1 && !dp[x][y]) {
                    stack.add(new int[]{x,y});
                    dp[x][y] = true;
                }
            }
        }

    }








    /*int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    int m, n;
    boolean[][] vis = new boolean[210][210];
    public int solve (char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ret = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1' && !vis[i][j]) {
                    ret ++ ;
                    dfs(grid, i, j);
                }
            }
        }
        return ret;
    }

    public void dfs(char[][] grid, int i, int j) {
        vis[i][j] = true;
        for (int a = 0; a < 4; a++) {
            int x = i + dx[a], y = j + dy[a];
            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' && !vis[x][y]) {
                dfs(grid,x,y);
            }
        }
    }

*/
}
